using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace System.Resources.SqlResource
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    internal static class ObjectByteArrayConverter
    {
        internal static byte[] ObjectToByteArray(Object obj)
        {
            MemoryStream fs = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(fs, obj);
                return fs.ToArray();
            }
            catch (SerializationException )
            {
                throw;
            }
            finally
            {
                fs.Flush();
                fs.Close();
            }
        }

        internal static object ByteArrayToObject(Byte[] Buffer)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream(Buffer);
            try
            {
                return formatter.Deserialize(stream);
            }
            catch (SerializationException e)
            {
                throw e;
            }
            finally
            {
                stream.Flush();
                stream.Close();
            }
        }
    }
}
